import pymysql
import bcrypt
import random

def create_users():
    conn = pymysql.connect(host='localhost', user='root', password='123456', database='rabc_db', charset='utf8')
    cursor = conn.cursor()
    depts = ['计算机系', '数学系', '物理系']
    grades = ['2021', '2022', '2023']
    user_types = ['学生', '教师', '管理员']
    for i in range(1, 11):
        username = f'test{i}'
        real_name = f'test用户{i}'
        user_type = random.choice(user_types)
        student_no = f'ST{i:04d}' if user_type == '学生' else None
        staff_no = f'SF{i:04d}' if user_type != '学生' else None
        dept = random.choice(depts)
        grade = random.choice(grades) if user_type == '学生' else None
        password = '123456'
        salt = bcrypt.gensalt()
        password_hash = bcrypt.hashpw(password.encode('utf-8'), salt)
        cursor.execute(
            "INSERT INTO user (username, password_hash, salt, real_name, user_type, student_no, staff_no, dept, grade) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
            (username, password_hash.decode('utf-8'), salt.decode('utf-8'), real_name, user_type, student_no, staff_no, dept, grade)
        )
    conn.commit()
    cursor.close()
    conn.close()
    print("10个用户已创建。")

if __name__ == '__main__':
    create_users() 